package com.zr.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;

import com.zr.dao.PointPriceDao;
import com.zr.datamodel.PointPriceDM;
import com.zr.service.PointPriceService;

import net.sf.json.JSONObject;
@Repository("pointPriceDao")
public class PointPriceDaoImpl<T> extends BaseDaOImpl<T> implements PointPriceDao<T>{
	
	@Override
	public long findPointPriceCount() {
		// TODO Auto-generated method stub
		String hql="select count(*) from PointPriceDM";
		long count=0;
		Query query=this.getCurrentSession().createQuery(hql);
		List<Object> obj=query.list();
		count=(long) obj.get(0);
		System.out.println("count:"+count);
		System.out.println(obj);
		return count;
	}

	@Override
	public List<PointPriceDM> findPointPriceMsg(int page, int rows) {
		// TODO Auto-generated method stub
		String hql="from PointPriceDM";
		Query query=this.getCurrentSession().createQuery(hql);
		query.setFetchSize((page-1)*rows);
		query.setMaxResults(rows);
		List<PointPriceDM> pointPrice=query.list();
		return pointPrice;
	}
	@Override
	public long selectPointPriceByPointNameCount(String pointName) {
		// TODO Auto-generated method stub
		String hql="select count(*) from PointPriceDM p";
		String wherehql=hql;
		long count=0;
		if(pointName!=null){
			wherehql=hql+" where p.startName like '%" +pointName+"%' or p.pointName like '%" +pointName+"%'";
		}
		Query query=this.getCurrentSession().createQuery(wherehql);
		List<Object> obj=query.list();
		count=(long) obj.get(0);
		System.out.println("count:"+count);
		System.out.println(obj);
		return count;
	}
	@Override
	public List<PointPriceDM> selectPointPriceByPointNameMsg(String pointName,int page, int rows) {
		// TODO Auto-generated method stub
		String hql="from PointPriceDM p";
		String wherehql=hql;
		if(pointName!=null){
			wherehql=hql+" where p.startName like '%" +pointName+"%' or p.pointName like '%" +pointName+"%'";
		}
		Query query=this.getCurrentSession().createQuery(wherehql);
		query.setFetchSize((page-1)*rows);
		query.setMaxResults(rows);
		List<PointPriceDM> pointPrice=query.list();
		return pointPrice;
	}

	@Override
	public void addPointPrice(String startName, String pointName, int firstPrice, int nextPrice, int firstcPrice,
			int nextcPrice) {
		// TODO Auto-generated method stub
		PointPriceDM pointPriceDM=new PointPriceDM(startName,pointName,firstPrice,nextPrice,firstcPrice,nextcPrice);
		this.getCurrentSession().save(pointPriceDM);
	}

	@Override
	public void delPointPrice(int id) {
		// TODO Auto-generated method stub
		try {
			PointPriceDM pointPriceDM=(PointPriceDM)this.getCurrentSession().get(PointPriceDM.class, id);
			System.out.println(pointPriceDM);
			this.getCurrentSession().delete(pointPriceDM);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void updatePointPrice(PointPriceDM pointPrice) {
		// TODO Auto-generated method stub
		PointPriceDM pointPriceDM=(PointPriceDM)this.getCurrentSession().get(PointPriceDM.class, pointPrice.getId());
		pointPriceDM.setStartName(pointPrice.getStartName());
		pointPriceDM.setPointName(pointPrice.getPointName());
		pointPriceDM.setFirstPrice(pointPrice.getFirstPrice());
		pointPriceDM.setNextPrice(pointPrice.getNextPrice());
		pointPriceDM.setFirstcPrice(pointPrice.getFirstcPrice());
		pointPriceDM.setNextcPrice(pointPrice.getNextcPrice());
		this.getCurrentSession().update(pointPriceDM);
	}

}
